﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace OTDR_OPT
{
    /// <summary>
    /// 自定义控件，回看点设置
    /// </summary>
    public partial class LinearControl : UserControl
    {
        public LinearControl()
        {
            InitializeComponent();
        }

        //获取复查点
        public CheckPoint GetChecnPoint()
        {
            string checkString = CheckBox.Text.Trim();
            string limitString = LimitBox.Text.Trim();
            if(checkString == "" || limitString == "")
            {
                return null;
            }
            try
            {
                double check = Convert.ToDouble(checkString);
                double limit = Convert.ToDouble(limitString);
                CheckPoint checkPoint = new CheckPoint();
                checkPoint.CheckPower = check;
                checkPoint.Limit = limit;
                return checkPoint;
            }
            catch
            {
                return null;
            }
        }


        //加号点击时，向父Grid添加一个控件
        private void AddButtonClick(object sender,RoutedEventArgs args) {
            Thickness thisThickness = Margin;
            double thisHeight = ActualHeight;
            Grid parentGrid = (Grid)Parent;
            #region 在当前控件下面添加一个控件
            LinearControl linearControl = new LinearControl();
            parentGrid.Children.Add(linearControl);
            linearControl.HorizontalAlignment = HorizontalAlignment.Left;
            linearControl.VerticalAlignment = VerticalAlignment.Top;
            double left = thisThickness.Left;
            double top = thisThickness.Top + thisHeight + 5;
            double right = thisThickness.Right;
            double bottom = thisThickness.Bottom;
            //linearControl.Margin = new Thickness(left,top,right,bottom);
            //linearControl.Height = thisHeight;
            #endregion

            #region 移动当前控件下面的控件
            foreach(UIElement element in parentGrid.Children)
            {
                LinearControl control = element as LinearControl;
                Thickness controlThickness = control.Margin;
                double controlLeft = controlThickness.Left;
                double controlTop = controlThickness.Top;
                double controlRight = controlThickness.Right;
                double controlBottom = controlThickness.Bottom;
                if(controlTop >= top)
                {
                    control.Margin = new Thickness(controlLeft, controlTop+thisHeight+5, controlRight, controlBottom);
                }
            }
            #endregion
            linearControl.Margin = new Thickness(left, top, right, bottom);
            linearControl.Height = thisHeight;
        }


        //删除按钮点击时，删除一个控件
        private void RemoveButtonClick(object sender,RoutedEventArgs args) {
            Thickness thisThickness = Margin;
            Grid parentGrid = (Grid)Parent;
            double top = thisThickness.Top;
            double thisHeight = Height;
            if (parentGrid.Children.Count < 2)
            {
                return;
            }
            //删除控件
            parentGrid.Children.Remove(this);
            #region 移动当前控件下面的控件
            foreach (UIElement element in parentGrid.Children)
            {
                LinearControl control = element as LinearControl;
                Thickness controlThickness = control.Margin;
                double controlLeft = controlThickness.Left;
                double controlTop = controlThickness.Top;
                double controlRight = controlThickness.Right;
                double controlBottom = controlThickness.Bottom;
                if (controlTop >= top)
                {
                    control.Margin = new Thickness(controlLeft, controlTop - thisHeight - 5, controlRight, controlBottom);
                }
            }
            #endregion
        }


        //文本框内容发生变化时，如果内容不能转为double，显示红色
        private void TextValueChanged(object sender,TextChangedEventArgs args) {
            TextBox senderBox = sender as TextBox;
            string textValue = senderBox.Text.Trim();
            try
            {
                Convert.ToDouble(textValue);
                senderBox.Foreground = Brushes.Black;
            }
            catch
            {
                senderBox.Foreground = Brushes.Red;
            }
        }


        //自定义控件尺寸发生变化时，上面的子控件同步发生变化
        private void ChangedSize(object sender,SizeChangedEventArgs args) {
            //控件的总尺寸
            double height = ActualHeight;
            ControlWidth = height * 15;
            ContentSize = Math.Round((height - 10) / 1.27, 1); //字号
            PointLabelMargin = new Thickness(0,0,0,0); //第一个label的边距固定
            double pointBoxLeft = ContentSize * 3 + 20; //复查点文本框左边距
            PointBoxMargin = new Thickness(pointBoxLeft,1,0,1); //复查点边距
            BoxHeight = height - 2; //文本框高度
            BoxWidth = ContentSize * 3 + 10; //文本框宽度
            double limitLabelLeft = pointBoxLeft + BoxWidth + 10; //允差Label左边距
            LimitLableMargin = new Thickness(limitLabelLeft,0,0,0); //允差Label边距
            double limitBoxLeft = limitLabelLeft + ContentSize * 2 + 20; //允差文本框做边距
            LimitBoxMargin = new Thickness(limitBoxLeft,0,0,0);
            ButtonHeight = BoxHeight;
            ButtWidth = BoxWidth / 3;
            double addButtonLeft = limitBoxLeft + BoxWidth + 10;  //添加按钮左边距
            AddButtonMargin = new Thickness(addButtonLeft,0,0,0);
            double removeButtonLeft= addButtonLeft + ButtWidth + 10; //删除按钮左边距
            RemoveButtonMargin = new Thickness(removeButtonLeft,0,0,0);
        }


        //复查点文本内容
        static DependencyProperty CheckPowerProperty = DependencyProperty.Register(
            "CheckPower",
            typeof(string),
            typeof(LinearControl)
            );

        public string CheckPower
        {
            set { SetValue(CheckPowerProperty,value); }
            get { return (string)GetValue(CheckPowerProperty); }
        }


        //规格线文本内容
        static DependencyProperty LimitProperty = DependencyProperty.Register(
            "Limit",
            typeof(string),
            typeof(LinearControl)
            );

        public string Limit
        {
            set { SetValue(LimitProperty,value); }
            get { return (string)GetValue(LimitProperty); }
        }


        //控件宽度
        static DependencyProperty ControlWidthProperty = DependencyProperty.Register(
            "ControlWidth",
            typeof(double),
            typeof(LinearControl)
            );

        private double ControlWidth
        {
            set { SetValue(ControlWidthProperty,value); }
            get { return (double)GetValue(ControlWidthProperty); }
        }


        //字体大小
        static DependencyProperty ContentSizeProperty = DependencyProperty.Register(
            "ContentSize",
            typeof(double),
            typeof(LinearControl)
        );

        private double ContentSize
        {
            set { SetValue(ContentSizeProperty, value); }
            get { return Convert.ToDouble(GetValue(ContentSizeProperty)); }
        }


        //复查点Label的边距
        static DependencyProperty PointLabelMarginProperty = DependencyProperty.Register(
            "PointLabelMargin",
            typeof(Thickness),
            typeof(LinearControl)
            );

        private Thickness PointLabelMargin
        {
            set { SetValue(PointLabelMarginProperty, value); }
            get { return (Thickness)GetValue(PointLabelMarginProperty); }
        }


        //复查点文本框的边距
        static DependencyProperty PointBoxMarginProperty = DependencyProperty.Register(
            "PointBoxMargin",
            typeof(Thickness),
            typeof(LinearControl)
            );

        private Thickness PointBoxMargin
        {
            set { SetValue(PointBoxMarginProperty,value); }
            get { return (Thickness)GetValue(PointBoxMarginProperty); }
        }


        //文本框的高度
        static DependencyProperty BoxHeightProperty = DependencyProperty.Register(
            "BoxHeight",
            typeof(double),
            typeof(LinearControl)
            );

        private double BoxHeight
        {
            set { SetValue(BoxHeightProperty,value); }
            get { return (double)GetValue(BoxHeightProperty); }
        }


        //文本框的宽度
        static DependencyProperty BoxWidthProperty = DependencyProperty.Register(
            "BoxWidth",
            typeof(double),
            typeof(LinearControl)
            );

        private double BoxWidth
        {
            set { SetValue(BoxWidthProperty,value); }
            get { return (double)GetValue(BoxWidthProperty); }
        }


        //允差Label边距
        static DependencyProperty LimitLableMarginProperty = DependencyProperty.Register(
            "LimitLableMargin",
            typeof(Thickness),
            typeof(LinearControl)
            );

        private Thickness LimitLableMargin
        {
            set { SetValue(LimitLableMarginProperty,value); }
            get { return (Thickness)GetValue(LimitLableMarginProperty); }
        }


        //允差文本框的边距
        static DependencyProperty LimitBoxMarginProperty = DependencyProperty.Register(
            "LimitBoxMargin",
            typeof(Thickness),
            typeof(LinearControl)
            );

        private Thickness LimitBoxMargin
        {
            set { SetValue(LimitBoxMarginProperty,value); }
            get { return (Thickness)GetValue(LimitBoxMarginProperty); }
        }


        //添加按钮边距
        static DependencyProperty AddButtonMarginProperty = DependencyProperty.Register(
            "AddButtonMargin",
            typeof(Thickness),
            typeof(LinearControl)
            );

        private Thickness AddButtonMargin
        {
            set { SetValue(AddButtonMarginProperty,value); }
            get { return (Thickness)GetValue(AddButtonMarginProperty); }
        }


        //删除按钮边距
        static DependencyProperty RemoveButtonMarginProperty = DependencyProperty.Register(
            "RemoveButtonMargin",
            typeof(Thickness),
            typeof(LinearControl)
            );

        private Thickness RemoveButtonMargin
        {
            set { SetValue(RemoveButtonMarginProperty,value); }
            get { return (Thickness)GetValue(RemoveButtonMarginProperty); }
        }


        //按钮高度
        static DependencyProperty ButtonHeightProperty = DependencyProperty.Register(
            "ButtonHeight",
            typeof(double),
            typeof(LinearControl)
            );

        private double ButtonHeight
        {
            set { SetValue(ButtonHeightProperty,value); }
            get { return (double)GetValue(ButtonHeightProperty); }
        }


        //按钮宽度
        static DependencyProperty ButtonWidthProperty = DependencyProperty.Register(
            "ButtonWidth",
            typeof(double),
            typeof(LinearControl)
            );

        private double ButtWidth
        {
            set { SetValue(ButtonWidthProperty,value); }
            get { return (double)GetValue(ButtonWidthProperty); }
        }
    }
}
